/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.coupon.repository;

import cn.icanci.coupon.entity.CouponEntity;
import cn.icanci.coupon.mapper.CouponMapper;
import cn.icanci.coupon.mapper.CouponRecordMapper;
import cn.icanci.coupon.model.Coupon;
import cn.icanci.coupon.model.CouponRecord;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

/**
 * @author icanci
 * @date Created in 2021/05/01 10:02
 * @version 1.0
 */
@Repository
public class CouponRepository {
    @Resource
    CouponMapper       couponMapper;
    @Resource
    CouponRecordMapper couponRecordMapper;

    /**
     * 根据优惠券唯一键查询
     * @param couponNumber 优惠券
     * @param userId 用户id
     * @return 返回查询的结果
     */
    public CouponRecord findRecord(String couponNumber, Integer userId) {
        return couponRecordMapper.findOneByCouponNumberAndUserId(couponNumber, userId);
    }

    /**
     * 根据优惠券唯一键查询
     * @param couponNumber 优惠券
     * @return 返回查询的结果
     */
    public Coupon findByCouponNumber(String couponNumber) {
        return couponMapper.findOneByCouponNumber(couponNumber);
    }

    /**
     * 根据优惠券唯一键查询
     * @param userId 用户id
     * @return 返回查询的结果
     */
    public List<CouponRecord> findRecordByUserId(Integer userId) {
        return couponRecordMapper.findManyByUserId(userId);
    }

    /**
     * 批量插入优惠券
     * @param couponList 优惠券列表
     */
    public int saveCoupons(List<Coupon> couponList) {
        return couponMapper.insertManyRows(couponList);
    }

    /**
     * 插入一条优惠券
     * @param couponEntity 优惠券实体
     * @param userId 用户id
     * @return 返回营销的行数
     */
    public int inertOneUserRecord(CouponEntity couponEntity, Integer userId) {
        CouponRecord couponRecord = new CouponRecord();
        BeanUtils.copyProperties(couponEntity, couponRecord);
        couponRecord.setCreateTime(new Date());
        couponRecord.setUpdateTime(new Date());
        couponRecord.setStatus(Integer.valueOf(2));
        couponRecord.setUserId(userId);
        return couponRecordMapper.inertOneUserRecord(couponRecord);
    }

    /**
     * 更新仓储
     * @param couponEntity
     * @return
     */
    public int updateStatus(CouponEntity couponEntity) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponEntity, coupon);
        return couponMapper.updateStatus(coupon);
    }

    /**
     * 更新
     * @param record
     * @return
     */
    public int updateCouponRecord(CouponRecord record) {
        record.setStatus(Integer.valueOf(3));
        return couponRecordMapper.updateCouponRecord(record);
    }

    /**
     * 根据 唯一key查询一条
     * @param uniqueKey
     * @return
     */
    public Coupon findByUniqueKey(String uniqueKey) {
        return couponMapper.queryOneByUnkLimit(uniqueKey);
    }

    /**
     * 根据 唯一key查询一条
     * @param couponNumber
     * @return
     */
    public Coupon queryOneByCouponNumber(String couponNumber) {
        return couponMapper.queryOneByCouponNumber(couponNumber);
    }
}
